﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FYP14
{
    public partial class Visit1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtBloodPressure.Attributes.Add("type", "number");
            txtSuger.Attributes.Add("type", "number");
            txtHeight.Attributes.Add("type", "number");
            txtWeight.Attributes.Add("type", "number");
            if (!IsPostBack)
            {
                Fill();
                GetAllAppointments();
                if (Request.QueryString["ID"] != null)
                {
                    SetValues(Convert.ToInt32(Request.QueryString["ID"]));
                }
            }
        }

        private void Fill()
        {
            DataTable dt;
            dt = BAL.GetAlllabtests();
            lstlabtest.DataSource = dt;
            lstlabtest.DataTextField = "labtest_type";
            lstlabtest.DataValueField = "labtest_id";
            lstlabtest.DataBind();

            //dt = BAL.GetAlllabtests();
            //lstlabtest.DataSource = dt;
            //lstlabtest.DataTextField = "";
            //lstlabtest.DataValueField = "";
            //lstlabtest.DataBind();

            dt = BAL.GetAllMedicines();
            lstmedicines.DataSource = dt;
            lstmedicines.DataTextField = "medicine_name";
            lstmedicines.DataValueField = "medicine_id";
            lstmedicines.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Visit oVisit = new Visit();
            Int32 VisitID = 0;
            oVisit.Appointment_ID = Convert.ToInt32(ddlAppointment.SelectedValue);
            oVisit.Visit_Date = Convert.ToDateTime(((TextBox)(dtcdate.FindControl("TextBox1"))).Text);
            
            if (Request.QueryString["ID"] != null)
            {
                oVisit.Visit_ID = Convert.ToInt32(Request.QueryString["ID"]);
                VisitID = oVisit.Visit_ID;
                BAL.UpdateVisit(oVisit);
            }
            else
            {
                VisitID = BAL.SaveVisit(oVisit);
            }

            VisitNote oVisitNote = new VisitNote();
            oVisitNote.Visit_ID = VisitID;
            oVisitNote.BloodPresuusre = txtBloodPressure.Text;
            oVisitNote.Suger = txtSuger.Text;
            oVisitNote.Height = txtHeight.Text;
            oVisitNote.Weight = txtWeight.Text;
            oVisitNote.Comments = txtcomments.Value;
            BAL.SaveVisitNote(oVisitNote);

            LabPrescription oLabPrescription = new LabPrescription();
            for (Int32 count = 0; count < lstlabtest.Items.Count; count++)
            {
                if (lstlabtest.Items[count].Selected)
                {
                    oLabPrescription.Visit_ID = VisitID;
                    oLabPrescription.LabTest_ID = Convert.ToInt32(lstlabtest.Items[count].Value);
                    BAL.SaveLabPrescription(oLabPrescription);
                }
            }

            MedicinePrescription oMedicinePrescription = new MedicinePrescription();
            for (Int32 count = 0; count < lstlabtest.Items.Count; count++)
            {
                if (lstmedicines.Items[count].Selected)
                {
                    oMedicinePrescription.Visit_ID = VisitID;
                    oMedicinePrescription.Medicine_ID = Convert.ToInt32(lstmedicines.Items[count].Value);
                    BAL.SaveMedicinePrescription(oMedicinePrescription);
                }
            }

            Response.Redirect("Visit.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Visit.aspx");
        }

        private void SetValues(Int32 ID)
        {
            DataTable dt = BAL.GetVisit(ID);
            if (dt.Rows.Count > 0)
            {
                ddlAppointment.Items[ddlAppointment.Items.IndexOf(ddlAppointment.Items.FindByValue(dt.Rows[0]["appointment_id"].ToString()))].Selected = true;
                ((TextBox)(dtcdate.FindControl("TextBox1"))).Text = dt.Rows[0]["visit_Date"].ToString();
            }
        }

        private void GetAllAppointments()
        {
            DataTable dt = BAL.GetAllAppointments();
            ddlAppointment.DataTextField = "appointment_type";
            ddlAppointment.DataValueField = "appointment_id";
            ddlAppointment.DataSource = dt;
            ddlAppointment.DataBind();
        }
    }
}